package com.zbec.mdrs.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.zbec.framework.core.util.PropertyUtil;
import com.zbec.mdrs.license.parser.License;
import com.zbec.mdrs.license.parser.LicenseParser;
import com.zbec.mdrs.repository.jpa.LicenseDao;
import com.zbec.mdrs.util.DateUtils;

@Component
@Transactional
public class LicenseRegisterService {
	private static Logger logger = LoggerFactory.getLogger(LicenseRegisterService.class);

	@Autowired
	private LicenseDao licenseDao;
	
	@Autowired
	private LicenseParser licenseParser;
	
	public String getClientUniqueIdentity(){
		return PropertyUtil.getInstance().getProperty("client.identity");
	}
	
	public License getLicense() {
		License license = null;
		List<com.zbec.mdrs.entity.license.License> licList = (List<com.zbec.mdrs.entity.license.License>)licenseDao.findAllOrderByIdDesc();
		if(licList == null || licList.size() < 1){
			logger.info("Hasn't any license in the table t_license.");
			return null;
		}
		com.zbec.mdrs.entity.license.License lic = licList.get(0);
		if(lic == null 
				|| lic.getLicenseText() == null 
				|| StringUtils.isBlank(StringUtils.trim(lic.getLicenseText()))){
			logger.info("The latest license is null or blank in the table t_license.");
			return null;
		}
		try {
			license = licenseParser.getLicense(StringUtils.trim(lic.getLicenseText()));
		} catch (Exception e) {
			logger.info("Parse the license error.", e);
			license = null;
		}
		if(license == null || license.getStartDate() == null || license.getExpireInDays() == null){
			logger.info("Can't parser the license from the encode string.");
			return null;
		}
		logger.info("After parser the license ="+ToStringBuilder.reflectionToString(license));
		return license;
	}
	
	public boolean checkLicense(String licenseText){
		License license = null;
		try {
			license = licenseParser.getLicense(licenseText);
		} catch (Exception e) {
			logger.info("Can't parser the license from the encode string, meets exception.", e);
			return true;
		}
		if(license == null || license.getStartDate() == null || license.getExpireInDays() == null){
			logger.info("Can't parser the license from the encode string.");
			return true;
		}
		Date expireDate = DateUtils.addDaysToDate(
				DateUtils.toDate(license.getStartDate(), "yyyyMMdd"),
				Integer.parseInt(license.getExpireInDays()));
		if (DateUtils.compareDates(expireDate, new Date()) < 0) {
			return true;
		}
		return false;
	}
	
	public void saveLicense(String licenseText){
		com.zbec.mdrs.entity.license.License license = new com.zbec.mdrs.entity.license.License();
		license.setLicenseText(licenseText);
		licenseDao.save(license);
	}
	
}
